package com.study.talk.data.repository

import android.util.Log
import com.study.common.annotation.Desc
import com.study.talk.data.local.UserInfoDao
import com.study.talk.data.model.User
import com.study.talk.data.model.UserInfo
import com.study.talk.data.model.vo.PasswordVo
import com.study.talk.data.model.vo.RegisterUserVo
import com.study.talk.data.remote.FriendApi
import com.study.talk.data.remote.UserApi
import javax.inject.Inject


class UserRepository @Inject constructor(
    private val userApi: UserApi,
    private val userInfoDao: UserInfoDao,
    private val friendApi: FriendApi
) : BaseRepository() {

    companion object {
        private const val TAG = "UserRepository"
    }

    @Desc("获取用户信息,优先缓存")
    suspend fun getUserInfo(userId: Int): UserInfo? {
        if (userInfoDao.has(userId)) {
            return userInfoDao.query(userId)
        } else {
            val response = userApi.getUserInfo(userId)

            if (response.isSuccessful) {
                if (response.body()?.data == null) {
                    Log.e(TAG, "getUserInfo: response.body()?.data is null")
                    return null
                } else {
                    userInfoDao.insert(response.body()?.data)
                    return response.body()?.data
                }
            } else {
                Log.e(TAG, "request fail,msg:${response.message()}")
                return null
            }
        }
    }

    @Desc("从网络获取用户信息")
    suspend fun requestUserInfo(userId: Int): UserInfo? {
        val response = userApi.getUserInfo(userId)
        if (response.isSuccessful) {
            if (response.body()?.data == null) {
                Log.e(TAG, "requestUserInfo: response.body()?.data is null")
                return null
            } else {
                if (userInfoDao.has(userId)) {
                    userInfoDao.update(response.body()?.data)
                } else {
                    userInfoDao.insert(response.body()?.data)
                }
                return response.body()?.data
            }
        } else {
            Log.e(TAG, "request fail,msg:${response.message()}")
            return null
        }
    }

    @Desc("查询是否为自己好友")
    suspend fun friend(userId: Int): Boolean? {
        val response = friendApi.friend(userId)
        if (response.isSuccessful) {
            if (response.body()?.data == null) {
                Log.e(TAG, "friend: response.body()?.data is null")
                return null
            } else {
                return response.body()?.data
            }
        } else {
            Log.e(TAG, "request fail,msg:${response.message()}")
            return null
        }
    }

    @Desc("删除好友")
    suspend fun deleteFriend(userId: Int): Boolean? {
        val response = friendApi.deleteFriend(userId)
        if (response.isSuccessful) {
            if (response.body()?.data == null) {
                Log.e(TAG, "deleteFriend: response.body()?.data is null")
                return null
            } else {
                return response.body()?.data
            }
        } else {
            Log.e(TAG, "request fail,msg:${response.message()}")
            return null
        }
    }

    @Desc("通过关键词查询用户")
    suspend fun searchUsers(keyword: String): List<UserInfo> {
        val response = userApi.searchUsers(keyword)
        if (response.isSuccessful) {
            if (response.body()?.data == null) {
                Log.e(TAG, "searchUsers: response.body()?.data is null")
                return listOf()
            } else {
                return response.body()?.data!!
            }
        } else {
            Log.e(TAG, "request fail,msg:${response.message()}")
            return listOf()
        }
    }

    @Desc("更新用户信息")
    suspend fun updateUserInfo(userInfo: UserInfo): UserInfo? {
        val response = userApi.updateUserInfo(userInfo)
        if (response.isSuccessful) {
            if (response.body()?.data == null) {
                Log.e(TAG, "updateUserInfo: response.body()?.data is null")
                return null
            } else {
                return response.body()?.data!!
            }
        } else {
            Log.e(TAG, "request fail,msg:${response.message()}")
            return null
        }
    }

    suspend fun registerUser(registerUserVo: RegisterUserVo, fail: (String?) -> Unit): User? {
        val response = userApi.registerSimple(registerUserVo)
        if (response.isSuccessful) {
            if (response.body()?.status == 200) {
                if (response.body()?.data == null) {
                    Log.e(TAG, "updateUserInfo: response.body()?.data is null")
                    return null
                } else {
                    return response.body()?.data!!
                }
            } else {
                fail(response.body()?.message)
                Log.e(TAG, "request fail,msg:${response.body()?.message}")
                return null
            }
        } else {
            Log.e(TAG, "request fail,msg:${response.message()}")
            return null
        }
    }

    @Desc("修改密码")
    suspend fun updatePwd(passwordVo: PasswordVo, fail: (String?) -> Unit): Boolean {
        val response = userApi.updatePwd(passwordVo)
        if (response.isSuccessful) {
            if (response.body()?.status == 200) {
                if (response.body()?.data == null) {
                    Log.e(TAG, "updateUserInfo: response.body()?.data is null")
                    return false
                } else {
                    return response.body()?.data!!
                }
            } else {
                fail(response.body()?.message)
                Log.e(TAG, "request fail,msg:${response.body()?.message}")
                return false
            }
        } else {
            Log.e(TAG, "request fail,msg:${response.message()}")
            return false
        }
    }
}